<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @property product $product
 * @property category $category
 * @property district $district
 * @property product_image $product_image
 * @property province $province
 * @property user $user
 * @property area_unit $area_unit
 * @property price_unit $price_unit
 * @property orientation $orientation
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 * @property CI_Benchmark $benchmark
 * @property CI_Calendar $calendar
 * @property CI_Cart $cart
 * @property CI_Config $config
 * @property CI_Controller $controller
 * @property CI_Email $email
 * @property CI_Encrypt $encrypt
 * @property CI_Exceptions $exceptions
 * @property CI_Form_validation $form_validation
 * @property CI_Ftp $ftp
 * @property CI_Hooks $hooks
 * @property CI_Image_lib $image_lib
 * @property CI_Input $input
 * @property CI_Lang $lang
 * @property CI_Loader $load
 * @property CI_Log $log
 * @property CI_Model $model
 * @property CI_Output $output
 * @property CI_Pagination $pagination
 * @property CI_Parser $parser
 * @property CI_Profiler $profiler
 * @property CI_Router $router
 * @property CI_Session $session
 * @property CI_Sha1 $sha1
 * @property CI_Table $table
 * @property CI_Trackback $trackback
 * @property CI_Typography $typography
 * @property CI_Unit_test $unit_test
 * @property CI_Upload $upload
 * @property CI_URI $uri
 * @property CI_User_agent $user_agent
 * @property CI_Validation $validation
 * @property CI_Xmlrpc $xmlrpc
 * @property CI_Xmlrpcs $xmlrpcs
 * @property CI_Zip $zip
 * @property CI_Javascript $javascript
 * @property CI_Jquery $jquery
 * @property CI_Utf8 $utf8
 * @property CI_Security $security
 * @property information $information
 * @property check_in_log $check_in_log
 */
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load_lib();

        $this->load_model();

        session_start();

        $this->detect_language();
    }

    public function detect_language()
    {
        $uri = $this->uri->uri_string();
        $segment = explode('/',$uri);
        $segment = $segment[0];
        if( $segment == 'vina-wealth-profile')
        {
            $this->session->set_userdata('lang','en');
            $this->lang->load('main','english');
        }
        else
        {
            $this->session->set_userdata('lang','vi');
            $this->lang->load('main','vietnamese');
        }
    }

    public function get_current_language()
    {
        return $this->session->userdata('lang');
    }

    public function connect_db()
    {
        $config['hostname'] = "localhost";
        $config['dbdriver'] = "mysql";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";


        // Localhost
        if(strpos(base_url(),'localhost') !== false)
        {

            $config['username'] = "root";
            $config['password'] = "root";
            $config['database'] = "escape_checkin";
        }
        else
        {
            $config['username'] = "root";
            $config['password'] = "root";
            $config['database'] = "escape_checkin";
        }
        $this->load->database($config);
    }

    public function load_lib()
    {
        $this->load->helper('language');
        $this->load->helper('url');
        $this->load->helper('date');
        $this->load->library('session');
        $this->load->library('uri');
        $this->load->helper('email');
        $this->load->library('email');
        $this->load->helper('file');
        $this->load->helper('download');
        $this->load->helper('utility');
        $this->load->helper('security');

        $this->connect_db();

        $upload_config['upload_path'] = 'uploads/';
        $upload_config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
        $this->load->library('upload', $upload_config);
    }

    public function load_model()
    {
        $this->load->model("user");
        $this->load->model("information");
        $this->load->model("check_in_log");

    }

    function array_to_object($array)
    {
        if(!is_array($array))
        {
            return $array;
        }

        $object = new stdClass();
        if (is_array($array) && count($array) > 0)
        {
            foreach ($array as $name=>$value) {
                $name = strtolower(trim($name));
                if (!empty($name)) {
                    $object->$name = $this->array_to_object($value);
                }
            }
            return $object;
        }
        else
        {
            return FALSE;
        }
    }

    public function send_mail($from,$to,$name,$subject,$message)
    {
        /*$config[]= array('protocol'=>'smtp',
                    'smtp_host'=>'smtp.integration.vn',
                    'smtp_user'=>'trongduc.tran@integration.vn',
                    'smtp_pass'=>'integrationgroup',
                    'smtp_port'=>'',
                    'mailtype' =>'html',
                    'charset' => 'utf-8');*/

        $config[]= array('protocol'=>'smtp',
                  'smtp_host'=>'localhost',
                  'smtp_user'=>'',
                  'smtp_pass'=>'',
                  'smtp_port'=>'',
                  'mailtype' =>'html',
                  'charset' => 'utf-8');

        $this->email->initialize($config);

        $this->email->set_newline("\r\n");

        //From email and sender name
        $this->email->from($from,$name);

        //To addresses. if more than once, add it as comma separated
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
    }
}